<?php
App::uses('AppController', 'Controller');
/**
 * Criterios Controller
 *
 * @property Criterio $Criterio
 */
class CriteriosController extends AppController {
	
public $uses = array( 'Criterio', 'Plantilla');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Criterio->recursive = 0;
		$this->set('criterios', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Criterio->exists($id)) {
			throw new NotFoundException(__('Criterio inválido'));
		}
		$options = array('conditions' => array('Criterio.' . $this->Criterio->primaryKey => $id));
		$this->set('criterio', $this->Criterio->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Criterio->create();
			if ($this->Criterio->save($this->request->data)) {
				$this->Session->setFlash(__('El criterio ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar el criterio. Por favor intente denuevo.'));
			}
		}

		$plantillas = $this->Criterio->Plantilla->find('list');
		$this->set(compact('plantillas'));
	}
	/**
 * add method
 *
 * @return void
 */
	public function add_criterio() {
		if ($this->request->is('post')) {
			$this->Criterio->create();
			if ($this->Criterio->save($this->request->data)) {
				$this->Session->setFlash(__('El criterio ha sido guardado'));
				$this->redirect('/plantillas/view/'.$this->data['Criterio']['plantilla_id']);
			} else {
				$this->Session->setFlash(__('No se ha podido guardar el criterio. Por favor intente denuevo.'));
			}
		}

		$plantillas = $this->Criterio->Plantilla->find('list');
		$this->set(compact('plantillas'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Criterio->exists($id)) {
			throw new NotFoundException(__('Criterio inválido'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Criterio->save($this->request->data)) {
				$this->Session->setFlash(__('El criterio ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar el criterio. Por favor intente denuevo.'));
			}
		} else {
			$options = array('conditions' => array('Criterio.' . $this->Criterio->primaryKey => $id));
			$this->request->data = $this->Criterio->find('first', $options);
		}
		$plantillas = $this->Criterio->Plantilla->find('list');
		$this->set(compact('plantillas'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Criterio->id = $id;
		if (!$this->Criterio->exists()) {
			throw new NotFoundException(__('Criterio inválido'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Criterio->delete()) {
			$this->Session->setFlash(__('riterio eliminado'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Criterio no fue eliminado'));
		$this->redirect(array('action' => 'index'));
	}
}
